﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using ShellCommandService.Contracts.DataContracts;
using ShellCommandService.Contracts.ServiceContracts;
using ShellCommandService.MSMQ;

namespace ShellCommandService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class PushShellCommand : IPushShellCommand
    {
        public void NormalFunction(string machineName)
        {
            IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();
            
                if (machineName != string.Empty)
                {
                    ShellCommandQueueRepository commandQueueRepository = ShellCommandQueueRepository.Instance;
                    List<ShellCommandInfo> shellCommands = commandQueueRepository.FetchUnexecutedCommands(machineName);
                    foreach (var shellCommandInfo in shellCommands)
                    {
                        callback.CallBackFunction(shellCommandInfo);
                        commandQueueRepository.RemoveTheMessageFromTheQueue(shellCommandInfo.MessageId);
                    }
                }
        }
    }
}
